<template>
	<view class="recommend-line-wrap relative">
		<loading-vue :loading="loading" text="正在加载" />
		<u-transition :show="!loading">
			<view class="recommend-line-wrap-inner">
				<view class="recommend-line-wrap-inner-wrap">
					<view class="water-full-col ">
						<view class="line-item" v-for="(item) in listLeft" :key="item.id" @click="toDetail(item.id)">
							<view class="line-image">
								<image :src="item.image" class="w-100" mode="widthFix"></image>
							</view>
							<view class="line-desc">
								<view class="lien-remark">
									<text>{{ item.desc }}</text>
								</view>
								<view class="user-wrap">
									<image :src="item.avatar" class="user-avatar"></image>
									<text class="usernaem">{{ item.nickname }}</text>
								</view>
							</view>
						</view>
					</view>
					<view class="water-full-col ">
						<view class="line-item" v-for="(item) in listRight" :key="item.id" @click="toDetail(item.id)">
							<view class="line-image">
								<image :src="item.image" class="w-100" mode="widthFix"></image>
							</view>
							<view class="line-desc">
								<view class="lien-remark">
									<text>{{ item.desc }}</text>
								</view>
								<view class="user-wrap">
									<image :src="item.avatar" class="user-avatar"></image>
									<text class="usernaem">{{ item.nickname }}</text>
								</view>
							</view>
						</view>
					</view>
				</view>

				<view class="empty-wrapper" v-if="isEmpty">
					<u-empty text="暂无数据" :icon="sourceUrl + '/data-empty.png'" />
				</view>
				<view class="loadmore-wrapper">
					<u-loadmore icon-color="#3c9cff" :status="status" loading-icon="semicircle" />
				</view>
			</view>
		</u-transition>
	</view>
</template>

<script>
	import LoadingVue from './loading.vue'
	import {
		LOAD_MORE
	} from '@/constant/index.js'
	export default {
		props: ['cityId', 'areaId'],
		components: {
			LoadingVue
		},
		created() {
			this.initData()
		},
		methods: {
			toDetail(id) {
				uni.navigateTo({
					url: '/pages-bundle/line-detail/line-detail?id=' + id
				})
			},
			patchList(data) {
				const list = {
					left: [],
					right: []
				}
				data.forEach((item, index) => {
					const {
						id,
						cover_image: image,
						introductory: desc,
						tourInfo: {
							avatar,
							nickname
						}
					} = item
					const target = {
						id,
						image,
						desc,
						avatar,
						nickname
					}
					if (index % 2 === 0) {
						list.left.push(target)
					} else {
						list.right.push(target)
					}
				})
				this.listLeft.push(...list.left)
				this.listRight.push(...list.right)
			},
			async initData(clear = false) {
				console.log(clear, 'clear');
				if (clear) {
					this.page = 1
					this.status = LOAD_MORE.LOADING
					this.isEmpty = false
					this.listLeft = []
					this.listRight = []
					this.loading = true
				}
				if (this.status === LOAD_MORE.NO_MORE) return
				const {
					page,
					pageSize,
					cityId,
					areaId
				} = this
				const resp = await this.$request({
					url: "/ProductLine/getLineLists",
					data: {
						page,
						list_rows: pageSize,
						city_id: cityId,
						area: areaId
					},
				})
				console.log(resp, 'resp');
				await this.$delay(300)
				const {
					code,
					data,
					msg
				} = resp
				if (code !== 1) {
					this.status = LOAD_MORE.LOAD_MORE
					return this.$toast({
						title: msg
					})
				}
				const {
					total,
					data: list
				} = data
				if (!!list && list.length) {
					this.patchList(list)
				}
				this.isEmpty = this.listLeft.length === 0
				if (this.loading) this.loading = false
				const hasMore = page * pageSize < total
				if (hasMore) {
					this.page += 1
					this.status = LOAD_MORE.LOAD_MORE
					return
				}
				this.status = LOAD_MORE.NO_MORE
			},
		},
		data() {
			return {
				loading: true,
				page: 1,
				pageSize: 15,
				listLeft: [],
				listRight: [],
				isEmpty: false,
				status: LOAD_MORE.LOAD_MORE,
			}
		}
	}
</script>

<style lang="scss" scoped>
	.recommend-line-wrap {
		.recommend-line-wrap-inner {
			.recommend-line-wrap-inner-wrap {
				position: relative;
				border-radius: 16rpx;
				padding: 28rpx 16rpx;
				box-sizing: border-box;
				display: flex;
				flex-flow: row nowrap;
				gap: 0 30rpx;
			}
		}

		.water-full-col {
			width: 100%;

			.line-item {
				position: relative;
				width: 100%;
				height: fit-content;
				border-radius: 16rpx;
				overflow: hidden;
				margin-bottom: 32rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.line-desc {
					padding: 16rpx;
					background-color: #ffffff;

					.user-wrap {
						margin-top: 8rpx;
						display: flex;
						align-items: center;

						.user-avatar {
							width: 32rpx;
							height: 32rpx;
							border-radius: 50%;
						}

						.usernaem {
							height: 32rpx;
							font-size: 20rpx;
							color: rgba(0, 0, 0, 0.6);
							margin-left: 8rpx;
							line-height: 32rpx;
						}
					}

					.lien-remark {
						font-size: 24rpx;
						color: rgba(0, 0, 0, 0.9);
						font-weight: bold;
						word-break: break-all;
						line-height: 40rpx;
					}
				}

				.line-image {
					width: 100%;
					font-size: 0;
				}
			}
		}
	}
</style>